import pygame
from random import randint
from my_sprites import *
pygame.init()
#创建时钟对象
clock = pygame.time.Clock()
screen = pygame.display.set_mode((480,700))

bg = pygame.image.load("./images/bj.png")
bg1 = pygame.image.load("./images/bj.png")
screen.blit(bg,(0,0))
screen.blit(bg,(-700,0))
user = pygame.image.load('./images/user.png')
screen.blit(user,(200,500))
hero = pygame.Rect(200,500,79,79)
#print(f'人的高度{hero_rect.height},宽度{hero_rect.width}')
pygame.display.update()

names = locals()
#创建精灵
for i in range(10) :
    names['enemy'+str(i)] = GameScripte("./images/hat.png",randint(2,5))
    names['enemy' + str(i)].rect.x = randint(1,470)
print(names)
#创建精灵组

enemy_group = pygame.sprite.Group(enemy0,enemy1,enemy2,enemy3,enemy4,enemy5,enemy6,enemy7,enemy8,enemy9)

flag = True
while True :
    #捕获事件
    for even in pygame.event.get() :
        if even.type == pygame.QUIT :
            print('退出…')
            pygame.quit()
            exit()
    clock.tick(60)
    hero.y -= 1
    if flag :
     hero.x -= 4
    else  :
       hero.x += 4

    if hero.x <= 0 :
        flag = False
    if hero.x >= 401 :
        flag = True

    if hero.y <= -49 :
        hero.y = 700

    screen.blit(bg, (0,0))
    screen.blit(user,hero)
    #精灵组调用两个方法 update drow
    enemy_group.update()
    enemy_group.draw(screen)



    pygame.display.update()

pygame.quit()